package com.aiobc.business.task;

import com.aiobc.business.dataext.domain.DataInfoExt;
import com.aiobc.business.dataext.mapper.DataInfoExtMapper;
import com.aiobc.business.personalcenter.domain.DataCommentLog;
import com.aiobc.business.personalcenter.mapper.DataCommentLogMapper;
import com.aiobc.common.core.constant.CacheConstants;
import com.aiobc.common.redis.service.RedisService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.apache.poi.ss.formula.functions.T;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author MK
 * @Date 2021-10-22 9:35
 */
public class UpdateExtTask {

    private static final Logger logger = LoggerFactory.getLogger(UpdateExtTask.class);
    public BigDecimal scoreSum = null;

    @Resource
    RedisService redisService;
    @Resource
    DataInfoExtMapper dataInfoExtMapper;
    @Resource
    DataCommentLogMapper dataCommentLogMapper;


    @XxlJob("updateDataExt")
    public ReturnT<String> updateDataExt(){
        try {
            List<DataInfoExt> dataInfoExtList = dataInfoExtMapper.selectList(null);
            dataInfoExtList.forEach(dataInfoExt -> {
                String key = CacheConstants.DATA_INFO_EXT_UPDATE + ":" + dataInfoExt.getDataId();
                if (redisService.hasKey(key)) {
                    Map<String, Long> dataInfoExtMap = redisService.getCacheMap(key);
                    dataInfoExt.setViewNum(dataInfoExt.getViewNum() + dataInfoExtMap.get(CacheConstants.VIEW_NUM));
                    dataInfoExt.setDownloadNum(dataInfoExt.getDownloadNum() + dataInfoExtMap.get(CacheConstants.DOWNLOAD_NUM));
                    dataInfoExt.setCallNum(dataInfoExt.getCallNum() + dataInfoExtMap.get(CacheConstants.CALL_NUM));
//                    dataInfoExt.setScore(getScore(dataInfoExt.getDataId()));
                    if (dataInfoExtMapper.updateDataInfoExt(dataInfoExt) == 1) {
                        redisService.deleteObject(key);
                    }else{
                        logger.error("数据更新未成功：{}",dataInfoExt);
                    }
                }else {
                    logger.info("不存在此键:{}",key);
                }
            });
        }catch (Exception e){
            logger.error("处理异常：{}",e);
            return ReturnT.FAIL;
        }
        return ReturnT.SUCCESS;
    }

    @XxlJob("updateScore")
    public ReturnT updateScore(){
        try {
            List<DataInfoExt> dataInfoExtList = dataInfoExtMapper.selectList(null);
            dataInfoExtList.forEach(dataInfoExt -> {
                dataInfoExt.setScore(getScore(dataInfoExt.getDataId()));
                dataInfoExtMapper.updateDataInfoExt(dataInfoExt);
            });
        }catch (Exception e){
            logger.error("处理异常：{}",e);
            return ReturnT.FAIL;
        }
        return ReturnT.SUCCESS;
    }

    /**
     * 计算评分平均值
     * @param dataId
     * @return
     */
    public BigDecimal getScore(String dataId){
        QueryWrapper<DataCommentLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("data_id",dataId);
        List<DataCommentLog> commentLogs = dataCommentLogMapper.selectList(queryWrapper);
        commentLogs.forEach(data->{
            scoreSum = scoreSum.add(BigDecimal.valueOf(Double.valueOf(data.getScore())));
        });
        return scoreSum.divide(BigDecimal.valueOf(commentLogs.size()));
    }
}
